package com.quantum.util
{
	import mx.utils.URLUtil;
	import mx.core.FlexGlobals;
	public class NetAddressUtils
	{
		/**
		 * Utility method to determine if the given url maps to
		 * localhost.
		 */
		public static function isLocalHost(ipOrUrl:String):Boolean
		{
			return ipOrUrl == "127.0.0.1" || ipOrUrl == "0:0:0:0:0:0:0:1" || ipOrUrl == "::1" || ipOrUrl == "localhost";
		}



		/**
		 * Returns the server name from the specified URL.
		 *
		 * @param url The URL to analyze.
		 * @return The server name of the specified URL.
		 *
		 * It is essentially the same as flex's URLUtil.getServerName, except it
		 * handles a bug relating to ipv6 address when the url is not a proper url
		 * but just the server name like [fc00:10:20:120:cc8f:b549:f80e8e47].
		 */
		public static function getServerNameFromUrl(url:String):String
		{
			var serverName:String=URLUtil.getServerName(url);
			if (StringUtils.isEmpty(serverName))
			{
				return serverName;
			}

			// There is a bug in URLUtil.getServerName, where if it is passed a ipv6 server
			// name which is not an actual url, then it strips off the opening square bracket.
			// e.g. [fc00:10:20:120:cc8f:b549:f80e8e47] becomes
			// fc00:10:20:120:cc8f:b549:f80e8e47]
			// The fix is to check if the server name ends with a square bracket and there is
			// no matching opening square bracket, then we add it.

			if (serverName.charAt(serverName.length - 1) == ']' && serverName.charAt(0) != '[')
			{
				serverName='[' + serverName;
			}
			return serverName;
		}
		
		public function currSwfUrl():void
		{  
			var currSwfUrl:String;
			var swfPath:String = FlexGlobals.topLevelApplication.stage.loaderInfo.url;//获取当前swf的路径
			var swfPathArray:Array = swfPath.split("/");  
			
			if (swfPathArray[0] == "file:") {  //本地路径
				if(swfPathArray.length<=3){  
					
					currSwfUrl = swfPathArray[2];  
					currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf(currSwfUrl.charAt(2)));  
				}
				else{  
					currSwfUrl = swfPath;  
					currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf("/"));  
				}  
			}else{  //网络路径
				currSwfUrl = swfPath;  
				currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf("/"));  
			}  
			
			currSwfUrl += "/";   
		}
		
	}
}